package com.alipay.app.groupmeal.standard.offline.manager;

import com.alipay.app.groupmeal.standard.offline.DbService;
import com.alipay.app.groupmeal.standard.offline.bean.OrderBean;
import com.alipay.app.groupmeal.standard.offline.bean.OrderBeanDao;

import org.greenrobot.greendao.query.QueryBuilder;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class OrderDataManager {

    private OrderBeanDao mDao;

    public static OrderDataManager getInstance() {
        DataManagerHolder.mInstance.mDao
                = DbService.getDaoSession().getOrderBeanDao();
        return DataManagerHolder.mInstance;
    }

    private static class DataManagerHolder {
        private static final OrderDataManager mInstance = new OrderDataManager();
    }

    public List<OrderBean> queryAllNotPosted() {
        QueryBuilder<OrderBean> queryBuilder = mDao.queryBuilder();
        return queryBuilder
                .where(OrderBeanDao.Properties.HavePost.eq(false))
                .build()
                .list();
    }

    public List<OrderBean> queryAllFailed() {
        QueryBuilder<OrderBean> queryBuilder = mDao.queryBuilder();
        return queryBuilder
                .where(OrderBeanDao.Properties.HavePost.eq(true),
                        OrderBeanDao.Properties.Success.eq(false))
                .build()
                .list();
    }

    public List<OrderBean> queryAll() {
        QueryBuilder<OrderBean> queryBuilder = mDao.queryBuilder();
        return queryBuilder
                .orderDesc(OrderBeanDao.Properties.CreateTime)
                .build()
                .list();
    }

    public List<OrderBean> queryAllLim() {
        QueryBuilder<OrderBean> queryBuilder = mDao.queryBuilder();
        return queryBuilder
                .orderDesc(OrderBeanDao.Properties.CreateTime)
                .limit(10)
                .build()
                .list();
    }

    public List<OrderBean> queryNoHavePostLim() {
        QueryBuilder<OrderBean> queryBuilder = mDao.queryBuilder();
        return queryBuilder
                .where(OrderBeanDao.Properties.HavePost.eq(false))
                .orderDesc(OrderBeanDao.Properties.CreateTime)
                .limit(10)
                .build()
                .list();
    }

    public List<OrderBean> queryCensus() throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        QueryBuilder<OrderBean> queryBuilder = mDao.queryBuilder();
        return queryBuilder
                .where(OrderBeanDao.Properties.Success.eq(true))
                .where(OrderBeanDao.Properties.CreateTime.ge(sdf.parse(sdf.format(new Date()) + " 00:00:00")))
                .build()
                .list();
    }


    /**
     * 新增
     *
     * @param bean
     * @return
     */
    public long insert(OrderBean bean) {
        return mDao.insert(bean);
    }

    public void delete(OrderBean bean) {
        mDao.deleteInTx(bean);
    }

    public void update(OrderBean bean) {
        mDao.update(bean);
    }

    public void upLoadSuccess(OrderBean bean) {
        bean.setHavePost(true);
        mDao.update(bean);
    }
}
